//
//  ViewController.m
//  MapTest
//
//  Created by xujun on 15/6/30.
//  Copyright (c) 2015年 xujun. All rights reserved.
//

#import "ViewController.h"
#import "KCAnnotation.h"




@interface ViewController ()
{


}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _geocoder=[[CLGeocoder alloc]init];
    
    //请求定位服务
    _locationManager=[[CLLocationManager alloc]init];
    if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
        [_locationManager requestWhenInUseAuthorization];
    }
    
    //用户位置追踪(用户位置追踪用于标记用户当前位置，此时会调用定位服务)
    _mapView.userTrackingMode=MKUserTrackingModeFollow;
    
    //设置地图类型
    _mapView.mapType=MKMapTypeStandard;
    
    CGRect rect=[UIScreen mainScreen].bounds;
    rect = CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2);
    _mapView=[[MKMapView alloc]initWithFrame:rect];
    [self.view addSubview:_mapView];
    //设置代理
    _mapView.delegate=self;
    

    //*****
    CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(70.00, 80.00);
    CLLocationCoordinate2D location2=CLLocationCoordinate2DMake(80.00, 90.00);
    CLLocationCoordinate2D location3=CLLocationCoordinate2DMake(40.00, 60.00);
    CLLocationCoordinate2D location4=CLLocationCoordinate2DMake(10.00, 50.00);

//    CGPoint p1 =  [_mapView convertCoordinate:location1 toPointToView:self.view];
//    CGPoint p2 =  [_mapView convertCoordinate:location2 toPointToView:self.view];
//    CGPoint p3 =  [_mapView convertCoordinate:location3 toPointToView:self.view];
//    CGPoint p4 =  [_mapView convertCoordinate:location4 toPointToView:self.view];


    tripPointCoordArry = [NSMutableArray arrayWithObjects:[NSValue valueWithMKCoordinate:location1],[NSValue valueWithMKCoordinate:location2],[NSValue valueWithMKCoordinate:location3],[NSValue valueWithMKCoordinate:location4], nil];
    
    
    [self caculatRegionByCoord];
    //添加大头针
    [self addAnnotation];

    [self showSceneRegion];

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    
}


-(void)showSceneRegion
{
    
    
   MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:regionAllScence];
    [_mapView setRegion:adjustedRegion animated:YES];

    
    //[_mapView setVisibleMapRect:mapVisibel];
    
    CGRect  fitRect = [_mapView convertRegion:adjustedRegion toRectToView:_mapView];
    
     //将地图视图的位置转换成地图的位置
    MKMapRect fitMapRect = [self convertRect:fitRect toMapRectFromView:_mapView];
    //设置地图可视范围为数据所在的地图位置
    //[_mapView setVisibleMapRect:fitMapRect animated:YES];

    //[self moveVisible];

    //将地图视图的位置转换成地图的位置，
//MKCoordinateRegion fitMapRect = [_mapView convertRect:fitRect toMapRectFromView:_mapView];
    //设置地图可视范围为数据所在的地图位置
    //[_mapView setVisibleMapRect:fitMapRect animated:YES];
    
}

-(MKMapRect)convertRect:(CGRect)fitMapRect toMapRectFromView:(MKMapView*)mapView
{
    MKMapRect zoomRect = MKMapRectMake(mapView.frame.origin.x, mapView.frame.origin.y, fitMapRect.size.width, fitMapRect.size.height);
    

    
    return zoomRect;
}

-(void)moveVisible
{
    MKMapRect zoomRect = MKMapRectNull;
    for (id <MKAnnotation> annotation in _mapView.annotations) {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(zoomRect)) {
            zoomRect = pointRect;
        } else {
            zoomRect = MKMapRectUnion(zoomRect, pointRect);
        }
    }
    [_mapView setVisibleMapRect:zoomRect animated:YES];
}


-(void)caculatRegionByCoord
{
   
    CLLocationCoordinate2D location1 = [[tripPointCoordArry objectAtIndex:0] MKCoordinateValue];
    CLLocationCoordinate2D location2 = [[tripPointCoordArry objectAtIndex:1] MKCoordinateValue];
    CLLocationCoordinate2D location3 = [[tripPointCoordArry objectAtIndex:2] MKCoordinateValue];
    CLLocationCoordinate2D location4 = [[tripPointCoordArry objectAtIndex:3] MKCoordinateValue];

    //取得维度范围
    NSMutableArray* latiArry = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:location1.latitude], [NSNumber numberWithDouble:location2.latitude],[NSNumber numberWithDouble:location3.latitude],[NSNumber numberWithDouble:location4.latitude],nil ];
    
    NSMutableArray *arrayLati= [latiArry sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 doubleValue] > [obj2 doubleValue] ) {
            return NSOrderedDescending;
        }
        if ([obj1 doubleValue] < [obj2 doubleValue] ) {
            return NSOrderedAscending;
        }
        
        return NSOrderedSame;
    }];
    
    
    CLLocationDegrees latitudeMin = (CLLocationDegrees)[arrayLati.firstObject doubleValue];
    CLLocationDegrees latitudeMax = (CLLocationDegrees)[arrayLati.lastObject doubleValue];
     CLLocationDegrees latitudSpan = latitudeMax -latitudeMin+10.0;
    
    
    //取得经度范围
    NSMutableArray* longtiArry = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:location1.longitude], [NSNumber numberWithDouble:location2.longitude],[NSNumber numberWithDouble:location3.longitude],[NSNumber numberWithDouble:location4.longitude],nil ];
    
    NSMutableArray *arraylong = [longtiArry sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 doubleValue] > [obj2 doubleValue] ) {
            return NSOrderedDescending;
        }
        if ([obj1 doubleValue] < [obj2 doubleValue] ) {
            return NSOrderedAscending;
        }
        
        return NSOrderedSame;
    }];
    
    
    CLLocationDegrees longMin = (CLLocationDegrees)[arraylong.firstObject doubleValue];
    CLLocationDegrees longMax = (CLLocationDegrees)[arraylong.lastObject doubleValue];
    CLLocationDegrees longSpan = longMax -longMin+10.0;
    
    //获得中心点
    CLLocationDegrees midLati = (latitudeMax +latitudeMin)/2.0f;
    CLLocationDegrees midlongti = (longMax+longMin)/2.0f;
    
    
    //获得区域
    MKCoordinateSpan span = MKCoordinateSpanMake(latitudSpan, longSpan);
    CLLocationCoordinate2D coordi = CLLocationCoordinate2DMake(midLati, midlongti);
    regionAllScence = MKCoordinateRegionMake(coordi, span);

    
    CGRect fitRect = [_mapView convertRegion:regionAllScence toRectToView:_mapView];

    //mapVisibel = MKMapRectMake(longMin, latitudeMin, span.longitudeDelta, span.latitudeDelta);
    
}

-(void) addViewMutile
{
    
    _pView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image"]];
    _pView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image"]];
    _pView3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image"]];

    
    
    
}

#pragma mark 添加大头针
-(void)addAnnotation{
    
    
    CLLocationCoordinate2D location1 = [[tripPointCoordArry objectAtIndex:0] MKCoordinateValue];
    CLLocationCoordinate2D location2 = [[tripPointCoordArry objectAtIndex:1] MKCoordinateValue];
    CLLocationCoordinate2D location3 = [[tripPointCoordArry objectAtIndex:2] MKCoordinateValue];
    CLLocationCoordinate2D location4 = [[tripPointCoordArry objectAtIndex:3] MKCoordinateValue];


    KCAnnotation *annotation1=[[KCAnnotation alloc]init];
    annotation1.title=@"CMJ Studio";
    annotation1.subtitle=@"Amy-1";
    annotation1.coordinate=location1;
    annotation1.rate=[UIImage imageNamed:@"image.png"];
    [_mapView addAnnotation:annotation1];

    
    KCAnnotation *annotation2=[[KCAnnotation alloc]init];
    annotation2.title=@"Amy-2";
    annotation2.coordinate=location2;
    annotation2.rate=[UIImage imageNamed:@"image.png"];
    [_mapView addAnnotation:annotation2];
    
    KCAnnotation *annotation3=[[KCAnnotation alloc]init];
    annotation3.title=@"Amy-3";
    annotation3.coordinate=location3;
    annotation3.rate=[UIImage imageNamed:@"image.png"];
    [_mapView addAnnotation:annotation3];
    
    
    KCAnnotation *annotation4=[[KCAnnotation alloc]init];
    annotation4.title=@"Amy-4";
    annotation4.coordinate=location4;
    annotation4.rate=[UIImage imageNamed:@"image.png"];
    [_mapView addAnnotation:annotation4];
}

@end
